package com.j180.controller.user;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.j180.bean.PageBean;
import com.j180.bean.ResultBean;
import com.j180.bean.User;
import com.j180.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService iUserService;

    /**
     * 用户登录
     * @param username 用户名
     * @param pwd 密码
     * @param request request对象
     * @return resultBean对象：
     *              code ：0 失败， 1成功
     *              msg：附加信息
     *              User：注册成功后的user对象
     */
    @GetMapping("/login/{username}/{pwd}")
    public ResultBean<User> userLogin(@PathVariable("username") String username, @PathVariable("pwd") String pwd, HttpServletRequest request){
        ResultBean<User> resultBean = new ResultBean<>();
        User u = iUserService.userLogin(username, pwd);

        if(u == null){
            resultBean.setT(null);
            resultBean.setCode(0);
            resultBean.setMsg("用户名或密码错误");
        }else if("不可用".equals(u.getState())){
            resultBean.setT(null);
            resultBean.setCode(0);
            resultBean.setMsg("该账号不可用，请让管理员更改状态");
        }else{
            resultBean.setCode(1);
            resultBean.setMsg("登录成功");
            resultBean.setT(u);
            request.getSession().setAttribute("user", u);
        }
        return resultBean;
    }

    /**
     *  用户注册
     * @param user 注册用户信息
     * @return resultBean:
     *          code: 0 失败， 1 成功
     *          msg：附加信息
     *          T：注册说明
     */
    @PostMapping("/register")
    public ResultBean<String> userRegister(@RequestBody(required = false) User user){
        ResultBean<String> resultBean = new ResultBean<>();
        System.out.println(user);
        int result = iUserService.userRegister(user);
        if(result == 0){
            resultBean.setCode(0);
            resultBean.setMsg("注册失败，原因未知");
            resultBean.setT("failure");
        }else{
            resultBean.setCode(1);
            resultBean.setMsg("注册成功");
            resultBean.setT("注册成功，等待管理员审核");
        }

        return resultBean;
    }

    /**
     * 修改用户状态
     * @param state 用户状态（可以，不可用，待审核）
     * @param id 用户id
     * @return resultBean:
     *           code: 0 失败， 1 成功
     *           msg：附加信息
     *           T：修改说明
     */
    @PutMapping("/update/{state}/{id}")
    public ResultBean<String> updateUserInfo(@PathVariable("state") String state, @PathVariable("id") int id){
        ResultBean<String> resultBean = new ResultBean<>();
        int result = iUserService.setUserState(state, id);

        if(result == 0) {
            resultBean.setCode(0);
            resultBean.setMsg("修改失败，原因未知");
            resultBean.setT("failure");
        } else {
            resultBean.setCode(1);
            resultBean.setMsg("修改成功");
            resultBean.setT("修改成功");
        }

        return resultBean;
    }

    /**
     * 分页查找用户信息
     * @param curPage 当前页数
     * @param pageSize 每页显示条数
     * @return resultBean:
     *      *code: 0 失败， 1 成功
     *      *msg：附加信息
     *      *T：分页显示的用户信息
     */
    @GetMapping("page/{curPage}/{pageSize}")
    public ResultBean<PageBean<User>> getUserByPage(
            @PathVariable("curPage") int curPage,
            @PathVariable("pageSize") int pageSize){
        ResultBean<PageBean<User>> pageUsers = new ResultBean<>();
        PageBean<User> pageBean = iUserService.getUserInfosByPage(curPage, pageSize);

        if(pageBean.getDatas() == null){
            pageUsers.setCode(0);
            pageUsers.setMsg("没有内容");
            pageUsers.setT(null);
        }else{
            pageUsers.setCode(1);
            pageUsers.setMsg("找到" + pageBean.getTolNum() + "条内容");
            pageUsers.setT(pageBean);
        }

        return pageUsers;
    }

    /**
     * 根据id查询用户信息
     * @param id 用户id
     * @return resultBean:
     *     code: 0 失败， 1 成功
     *     msg：附加信息
     *     T：用户信息，没有查到该用户则返回null
     */
    @GetMapping("get/{id}")
    public ResultBean<User> getUserById(@PathVariable("id") int id){
        ResultBean<User> user = new ResultBean<>();
        User u = iUserService.getUserInfoById(id);

        if(u == null){
            user.setCode(0);
            user.setMsg("没有id为" + id + "的用户信息");
            user.setT(null);
        }else{
            user.setCode(1);
            user.setMsg("找到了");
            user.setT(u);
        }

        return user;
    }

    /**
     * 修改User的信息
     * @param user User对象
     * @return
     *      code: 0 失败， 1 成功
     *      msg：附加信息
     *      T：修改说明
     */
    @RequestMapping(value = "edit", method = RequestMethod.PUT)
    public ResultBean<String> updateUserInfo(@RequestBody(required = false) User user){
        ResultBean<String> resultBean = new ResultBean<>();
        int result = iUserService.updateUserInfo(user);

        if(result == 0){
            resultBean.setCode(0);
            resultBean.setMsg("修改失败，原因未知");
            resultBean.setT("修改失败");
        }else{
            resultBean.setCode(1);
            resultBean.setMsg("修改成功");
            resultBean.setT("修改成功");
        }

        return resultBean;
    }

    /**
     * 根据id删除一个用户
     * @param id 用户id
     * @return resultBean:
     *          code: 0 失败， 1 成功
     *          msg：附加信息
     *          T：删除说明
     */
    @DeleteMapping("delete/{id}")
    public ResultBean<String> deleteUser(@PathVariable("id") int id){
        ResultBean<String> resultBean = new ResultBean<>();
        int result = iUserService.deleteUserById(id);

        if(result == 0) {
            resultBean.setCode(0);
            resultBean.setMsg("删除失败，原因未知");
            resultBean.setT("failure");
        } else {
            resultBean.setCode(1);
            resultBean.setMsg("删除成功");
            resultBean.setT("删除成功");
        }

        return resultBean;
    }

    /**
     * 根据用户状态查询用户
     * @param state 用户状态
     * @return resultBean:
     *        code: 0 失败， 1 成功
     *        msg：附加信息
     *        T：查询到的信息，没有则为null
     */
    @GetMapping("editState/{state}")
    public ResultBean<List<User>> editUserState(@PathVariable("state") String state){
        ResultBean<List<User>> resultBean = new ResultBean<>();
        List<User> users = iUserService.getRegisterUser(state);

        if(users.size() == 0){
            resultBean.setCode(0);
            resultBean.setMsg("没有内容");
            resultBean.setT(null);
        }else{
            resultBean.setCode(1);
            resultBean.setMsg("找到" + users.size() + "条内容");
            resultBean.setT(users);
        }

        return resultBean;
    }
}